package com.hjj.controller;

import com.hjj.beans.Stu;
import com.hjj.dto.ResultDTO;
import com.hjj.exception.MyDateExceptionAdvice;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

@RestController
@RequestMapping("/")
public class IndexController {

    @RequestMapping("get.action")
    public ResultDTO get(Stu stu, BindingResult bindingResult) throws MyDateExceptionAdvice {
        if (bindingResult.hasErrors()){
            throw new MyDateExceptionAdvice("生日格式输入错误");
        }

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("name", stu.getName());
//        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
//        String age = stu.getAge().format(dateTimeFormatter);
        hashMap.put("age", bindingResult.getFieldValue("age").toString());
        resultDTO.setData(hashMap);

        return resultDTO;
    }

}
